const { _getsecurityServer,_deletesecurityServer,_updatesecurityServer ,_addsecurityServer,_BatchdeletionSecurityServer,_selectsecurityServer} = require("../service/security");

module.exports = {
    // 获取数据的方法
    async _getsecurity(ctx){
        const {page,size} = ctx.query;
        const result = await _getsecurityServer(page,size)
        if(result.length){
            ctx.body={
                code:1,
                message:'获取成功',
                result
            }
        } else {
            ctx.body={
                code:0,
                message:'获取失败',
            }
        }
    },
    // 根据id删除单条数据的方法
    async _deletesecurity(ctx){
        const {id} = ctx.query;
        const result = await _deletesecurityServer(id)
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'删除成功'
            }
        } else{
            ctx.body={
                code:0,
                message:'删除失败'
            }
        }
    },
    // 根据id修改单条数据的方法
    async _updatesecurity(ctx){
        const {id,name,img,location,access,num} = ctx.request.body;
        const result = await _updatesecurityServer({id,name,img,location,access,num})
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'修改成功'
            }
        } else{
            ctx.body={
                code:0,
                message:'修改失败'
            }
        }
    },
    // 添加数据到数据库的方法
    async _addsecurity(ctx){
        const {name,img,location,access,num} = ctx.request.body;
        const result = await _addsecurityServer({name,img,location,access,num})
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'添加成功'
            }
        } else {
            ctx.body={
                code:0,
                message:'添加失败'
            }
        }       
    },
    // 批量删除的方法
    async _BatchdeletionSecurity(ctx){
        const {ids} = ctx.request.body;
        const result = await _BatchdeletionSecurityServer(ids)
        if(result.affectedRows){
            ctx.body={
                code:1,
                message:'批量删除成功'
            }
        } else {
            ctx.body={
                code:0,
                message:'批量删除失败'
            }
        }
    },
    // 多条件查询
    async _selectsecurity(ctx){
        const {name,address} = ctx.query;
        const result = await _selectsecurityServer(name,address)
        if(result.length){
            ctx.body={
                code:1,
                message:'查询成功',
                result
            }
        } else {
            ctx.body={
                code:0,
                message:'查询失败',
            }
        }
    }
    
}